//
//  ViewController.m
//  04-UIControl
//
//  Created by 曹曹 on 16/7/19.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UISegmentedControl *segment;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Switch];
    [self Slider];
    [self Segment];
    [self PageControl];
}
#pragma mark - UISwitch (valueChanged)
- (void)Switch {
    // switch的宽和高是固定的, 只能设定 x 和 y
    UISwitch *swich = [[UISwitch alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
    // 1.开关处于开启状态时内部的颜色
    swich.onTintColor = [UIColor redColor];
    // 2.边框及切换到关闭时的颜色 null_resettable 懒加载
    swich.tintColor = [UIColor blackColor];
    swich.tintColor = nil;//赋值为空时有默认颜色(灰)
    // 3.开关按钮(圆圈)的颜色
    swich.thumbTintColor = [UIColor purpleColor];
    // 4.打开时的图片, iOS 7以后失效
    swich.onImage = [UIImage imageNamed:@"1.JPG"];
    // 5.关闭时的图片, iOS 7以后失效
    swich.offImage = [UIImage imageNamed:@"2.JPG"];
    // 6.判断switch的开/闭状态, 默认是关闭
    NSLog(@"%@", swich.isOn ? @"YES" : @"NO");
    // 通过setter方法改变switch的初始状态
    swich.on = YES;
    [swich setOn:NO animated:YES];
    // 7.添加绑定事件, 与Button相同
    [swich addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:swich];
}
// switch触发的事件
- (void)handleSwitch:(UISwitch *)swich {
    // 以switch的状态为判断条件
    if (swich.isOn) {
        NSLog(@"开");
    } else {
        NSLog(@"关");
    }
}
#pragma mark - UISlider (valueChanged)
- (void)Slider {
    // slider的高度是固定的, 这个控件永远居中于视图
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 200, 400, 100)];
     slider.backgroundColor = [UIColor grayColor];
    // 1.当前滑块的值
    slider.value = 0.2;
    [slider setValue:0.5 animated:YES];
    // 2. 滑块最大值1.0f
    slider.maximumValue = 1.0f;
    // 3. 滑块最小值0.0f, 最大和最小值可以根据需求去改变
    slider.minimumValue = 0.0f;
    // 4. 设置为YES时持续触发事件, 设置NO时只在手指离开时触发事件
    slider.continuous = NO;
    // 5. 添加事件
    [slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
    // 6. 未滑过区域的颜色
    slider.maximumTrackTintColor = [UIColor redColor];
    // 7. 滑过区域的颜色(设置图片后将不显示该颜色)
    slider.minimumTrackTintColor = [UIColor greenColor];
    // 8. 滑块的颜色(设置滑块图片后不需要设置这个属性)
    // slider.thumbTintColor = [UIColor greenColor];
    // 9. 设置未滑过区域图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    // 10. 设置滑过区域的图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    // 11. 设置滑块的图片
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    // 12. 设置左侧图片
    slider.minimumValueImage = [UIImage imageNamed:@"voice_input_speaker"];
    // 13. 设置右侧图片
    slider.maximumValueImage = [UIImage imageNamed:@"shoppingcart"];
    [self.view addSubview:slider];
}
// slider触发的事件
- (void)handleSlider:(UISlider *)slider {
    NSLog(@"%f", slider.value);
}
#pragma mark - Segment (valueChanged)
- (void)Segment {
    _segment = [[UISegmentedControl alloc] initWithItems:@[@"你好", @"哈哈", @"欧好友", @"哇咔咔"]];
    // 不设置frame时系统会自动计算大小, 推荐加上frame
    NSLog(@"%@", NSStringFromCGRect(_segment.frame));
    _segment.frame = CGRectMake(10, 400, 400, 29);
    // 1.设置为YES时不会有选中效果, 是一种类似于UIButton的点击效果
    _segment.momentary = YES;
    [self.view addSubview:_segment];
    // 2.获得segment的个数
    NSInteger numberOfSegment = [_segment numberOfSegments];
    NSLog(@"%ld", numberOfSegment);
    // 3.修改对应下标的标题
    [_segment setTitle:@"阿黄啦啦啦啦" forSegmentAtIndex:0];
    // 4.设置对应下标的宽度, 默认值为0.0,表示自动调整宽度(均分)
    [_segment setWidth:150 forSegmentAtIndex:2];
    // 5.设置对应下标内容的偏移
    [_segment setContentOffset:CGSizeMake(10, 5) forSegmentAtIndex:2];
    // 6.设置对应下标是否能被选中
    [_segment setEnabled:NO forSegmentAtIndex:0];
    // 7.边框颜色
    _segment.tintColor = [UIColor redColor];
    // 8.设置/获取 选中的segment下标
    _segment.selectedSegmentIndex = 1;
    // 9.apportionsSegmentWidthsByContent, 默认为NO, 如果分段控制器的段宽为0则可以设置这个属为YES, 系统会自动计算内容的宽度
    _segment.apportionsSegmentWidthsByContent = YES;
    //
    [_segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addButton.frame = CGRectMake(100, 600, 100, 40);
    addButton.backgroundColor = [UIColor greenColor];
    [addButton setTitle:@"添加标题" forState:UIControlStateNormal];
    [self.view addSubview:addButton];
    [addButton addTarget:self action:@selector(handleAddButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handleAddButton:(UIButton *)button {
    [self.segment insertSegmentWithTitle:@"添加标题" atIndex:2 animated:YES];
}
- (void)handleSegment:(UISegmentedControl *)segment {
    NSLog(@"%ld", segment.selectedSegmentIndex);
    switch (segment.selectedSegmentIndex) {
        case 1: NSLog(@"case1"); break;
        case 2: NSLog(@"case2"); break;
        default:
            break;
    }
}
#pragma mark - PageControl (valueChanged)
- (void)PageControl {
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(150, 300, 71, 40)];
    pageControl.backgroundColor = [UIColor blackColor];
    // 1.分页控制器的页数
    pageControl.numberOfPages = 5;
    // 2.设置分页控制器的初始页
    pageControl.currentPage = 0;
    // 3.隐藏分页控制器当只有一页时, 默认为NO(始终显示)
    pageControl.hidesForSinglePage = YES;
    // 4.获取n页控制器的推荐尺寸(宽度和高度)
    CGSize size = [pageControl sizeForNumberOfPages:5];
    NSLog(@"%@", NSStringFromCGSize(size));
    // 5.设置未选中页颜色
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    // 6.设置选中页的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    // 7.添加事件
    [pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    // 8.设置为YES表示关闭自动切换, 直到调用了updateCurrentPageDisplay方法才会切换
    pageControl.defersCurrentPageDisplay = YES;
    [self.view addSubview:pageControl];
}
- (void)handlePageControl:(UIPageControl *)pageControl {
    NSLog(@"change");
    [pageControl updateCurrentPageDisplay];
}
#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
